package com.wangsd.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;


/**
 * api接口文档配置类
 */
@Configuration
@EnableSwagger2
@Profile({"dev", "test"})
public class SwaggerConfig {

    private ApiInfo apiInfo(String title, String description, String version) {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .version(version)
                .build();
    }

    //这是按包来分组
    /*@Bean
    public Docket api() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("x-access-token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("api组")
                .apiInfo(apiInfo("1","2","3"))
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.meike.station"))
                .paths(PathSelectors.any())
                .build();
    }*/

    //按路由分组
    @Bean
    public Docket createOutApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("外部调用接口")
                .apiInfo(apiInfo("标题：人脸支付系统_接口文档", "描述：外部接口", "1.0"))
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/api/**"))
                .build()
                .pathMapping("/");
    }

    @Bean
    public Docket createAdminApi() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("Access-Token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("pc端调用接口")
                .apiInfo(apiInfo("标题：交易上报管理界面接口", "描述：交易上报管理界面接口", "1.0"))
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/report/**"))
                .build()
                .pathMapping("/")
                .globalOperationParameters(pars);
    }

    @Bean
    public Docket createCommonApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("pc上传接口")
                .apiInfo(apiInfo("标题：交易上报文件上传接口", "描述：文件上传接口", "1.0"))
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/common/**"))
                .build()
                .pathMapping("/");
    }

    @Bean
    public Docket createSysApi() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("Access-Token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("pc端调用接口-系统配置")
                .apiInfo(apiInfo("标题：交易上报系统管理管理接口", "描述：pc端管理系统接口-系统配置", "1.0"))
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/sys/**"))
                .build()
                .pathMapping("/")
                .globalOperationParameters(pars);
    }

    @Bean
    public Docket createWxApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("微信接口")
                .apiInfo(apiInfo("标题：人脸支付系统_接口文档", "描述：微信接口", "1.0"))
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/wx/**"))
                .build()
                .pathMapping("/");
    }

}
